package com.villaday.idf.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Build;
import androidx.annotation.RequiresApi;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.villaday.idf.R;
import com.villaday.idf.activity3.CheckInvoiceActivity;
import com.villaday.idf.base.AbsRecyclerViewAdapter;
import com.villaday.idf.base.OnAdapterCallbackListener;
import com.villaday.idf.custom.SlideRecyclerView;
import com.villaday.idf.model.Activity;
import com.villaday.idf.model.PresellEntity;
import com.villaday.idf.model.behaviorsEntity;
import com.villaday.idf.model.ordersEntity;
import com.villaday.idf.util.PhoneUtil;
import com.villaday.idf.util.SharedPreferencesUtil;

import java.math.BigDecimal;

/**
 * 首页动态列表
 */

public class OrderAdapter extends AbsRecyclerViewAdapter<ordersEntity.BodyEntity.ListEntity> {
    private OnAdapterCallbackListener onAdapterCallbackListener;
    private View.OnClickListener onClickListener;
    private Context mContext;
    LinearLayout llContent;
    SlideRecyclerView slideRecyclerView;


    public OrderAdapter(Context context, OnAdapterCallbackListener onAdapterCallbackListener, View.OnClickListener onClickListener) {
        super(context, R.layout.item_order, R.layout.item_page_loading,
                R.layout.item_page_bottom);
        this.mContext = context;
        this.onAdapterCallbackListener = onAdapterCallbackListener;
        this.onClickListener = onClickListener;

    }

    public OrderAdapter(Context context, OnAdapterCallbackListener onAdapterCallbackListener, View.OnClickListener onClickListener, SlideRecyclerView slideRecyclerView) {
        super(context, R.layout.item_order, R.layout.item_page_loading,
                R.layout.item_page_bottom);
        this.mContext = context;
        this.onAdapterCallbackListener = onAdapterCallbackListener;
        this.onClickListener = onClickListener;
        this.slideRecyclerView = slideRecyclerView;

    }


    @Override
    public int getItemType(ordersEntity.BodyEntity.ListEntity d) {
        return d.getMyType();
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    public void onBindHolder(final AbsRecyclerViewAdapter.RecyclerViewHolder holder, final ordersEntity.BodyEntity.ListEntity d, final int position) {

/*        final DisplayMetrics metrics = new DisplayMetrics();
        ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(metrics);
        final int width = metrics.widthPixels;*/



/*
        rootView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
            @Override
            public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                d.setOpen(true);
                if (scrollX > tvDel.getWidth() / 2 && d.isOpen()) {
                    rootView.smoothScrollBy(width + tvDel.getWidth(), 0);
                } else {
                    rootView.smoothScrollBy(0, 0);
                }

            }
        });
*/

        switch (d.getMyType()) {

            case 0: {//

                final int width = PhoneUtil.getPhoneWidth(context);
                // 屏幕宽度（像素）
                llContent = (LinearLayout) holder.getView(R.id.ll_content_layout);
                llContent.setLayoutParams(new LinearLayout.LayoutParams(width, LinearLayout.LayoutParams.MATCH_PARENT));

                final TextView tvDel = (TextView) holder.getView(R.id.tv_del);


                tvDel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (onSwipeItemDelClickListener != null) {
                            onSwipeItemDelClickListener.delItem(position);
                        }
                    }
                });


                SimpleDraweeView sdv = (SimpleDraweeView) holder.getView(R.id.sdv_head);
                SimpleDraweeView img = (SimpleDraweeView) holder.getView(R.id.sdv);
                sdv.setVisibility(View.GONE);
                sdv.setImageURI(PhoneUtil.getHead(SharedPreferencesUtil.getRoleInfo(mContext).getMember().getHeaderUrl()));
                img.setImageURI(PhoneUtil.getHead(d.getProduct().getImageUrl()));


                holder.bindTextView(R.id.tv_name, /*SharedPreferencesUtil.getRoleInfo(mContext).getMember().getNickName()*/"订单编号：" + d.getOrderNo())
                        .bindTextView(R.id.tv_order_type, d.getStatusText())
                        .bindTextView(R.id.tv_title, d.getProduct().getName())
                        .bindTextView(R.id.tv_price, "¥" + d.getActualPriceValue())
                        .bindTextView(R.id.detail, d.getProduct().getDesc())
                        .setViewVisible(R.id.tv_other_operation,d.isHasInvoice()?View.VISIBLE:View.GONE)
                        .setOnClickListener(R.id.tv_into_order)
                        .setOnClickListener(R.id.ll_goods_layout)
                        .setOnClickListener(R.id.tv_other_operation)
                        .setOnClickListener(R.id.tv_del)
                        .setClickListener(R.id.tv_other_operation, new View.OnClickListener() {//查看发票
                            @Override
                            public void onClick(View v) {
                                Intent intent = new Intent(context, CheckInvoiceActivity.class);
                                intent.putExtra("orderNo",d.getOrderNo());
                                context.startActivity(intent);
                            }
                        });
                if (!d.isAllowDelete()) {
                    holder.setViewVisible(R.id.tv_del, View.GONE);
                    llContent.setLayoutParams(new LinearLayout.LayoutParams(width, LinearLayout.LayoutParams.MATCH_PARENT));
                    holder.setIsRecyclable(true);
                } else {
                    holder.setViewVisible(R.id.tv_del, View.VISIBLE);
                    holder.setIsRecyclable(false);
                }

                PhoneUtil.setExternalFont(context, (TextView) holder.getView(R.id.tv_price), true);
            }
            break;

            case 1: {
                onAdapterCallbackListener.onCallback();
            }
            break;


        }
    }


    public OnSwipeItemDelClickListener onSwipeItemDelClickListener;
    private boolean isAllowSwipe = false;

    /*    *//*
    public void removeItemByPosition(int position) {
        mdatas.remove(position);
        notifyItemRemoved(position);
        //重新排列数据
        notifyItemRangeChanged(position, mdatas.size());
    }*/


    public boolean isAllowSwipe() {
        return isAllowSwipe;
    }

    public void setAllowSwipe(boolean allowSwipe) {
        isAllowSwipe = allowSwipe;
    }

    public void setOnSwipeItemDelClickListener(OnSwipeItemDelClickListener onSwipeItemDelClickListener) {
        this.onSwipeItemDelClickListener = onSwipeItemDelClickListener;
    }

    interface OnSwipeItemDelClickListener {
        void delItem(int position);
    }

}
